﻿<UserControl
    x:Class="DaxStudio.UI.Views.DocumentView" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:avalondock="http://schemas.xceed.com/wpf/xaml/avalondock" 
    mc:Ignorable="d" d:DesignWidth="294.813" d:DesignHeight="215.84" 
    xmlns:my="clr-namespace:DaxStudio.UI"
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    xmlns:cal="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro.Platform"
    xmlns:dax="clr-namespace:DAXEditor;assembly=DAXEditor" 
    xmlns:beh="clr-namespace:DaxStudio.UI.Behaviours"
    xmlns:dd="clr-namespace:GongSolutions.Wpf.DragDrop;assembly=GongSolutions.Wpf.DragDrop"
    xmlns:local="clr-namespace:DaxStudio.UI.Utils"
    xmlns:attached="clr-namespace:DaxStudio.UI.AttachedProperties">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary  Source="pack://application:,,,/DaxStudio.UI;Component/Resources/IconResourceDictionary.xaml" />
            </ResourceDictionary.MergedDictionaries>
        
        <Style x:Key="CustomLayoutDocumentPaneControlStyle" TargetType="{x:Type avalondock:LayoutDocumentPaneControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type avalondock:LayoutDocumentPaneControl}">
                        <Grid ClipToBounds="True" SnapsToDevicePixels="True" KeyboardNavigation.TabNavigation="Local">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="0"/>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>
                            <Border Background="Transparent" Grid.RowSpan="2"/>
                            <Grid Panel.ZIndex="1">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition/>
                                    <ColumnDefinition Width="Auto"/>
                                </Grid.ColumnDefinitions>
                                <avalondock:DocumentPaneTabPanel x:Name="HeaderPanel" Grid.Column="0" FlowDirection="LeftToRight" IsItemsHost="True" 
                                                                 
                                                                 Grid.Row="0" KeyboardNavigation.TabIndex="1" />
                                    <!-- Margin="2,2,2,0" -->
                                <avalondock:DropDownButton x:Name="MenuDropDownButton" Grid.Column="1" Focusable="False" Visibility="Hidden">
                                    <avalondock:DropDownButton.DropDownContextMenu>
                                        <avalondock:ContextMenuEx>
                                            <avalondock:ContextMenuEx.ItemContainerStyle>
                                                <Style TargetType="{x:Type avalondock:MenuItemEx}">
                                                    <Setter Property="HeaderTemplate" Value="{Binding Root.Manager.DocumentPaneMenuItemHeaderTemplate}"/>
                                                    <Setter Property="HeaderTemplateSelector" Value="{Binding Root.Manager.DocumentPaneMenuItemHeaderTemplateSelector}"/>
                                                    <Setter Property="IconTemplate" Value="{Binding Root.Manager.IconContentTemplate}"/>
                                                    <Setter Property="IconTemplateSelector" Value="{Binding Root.Manager.IconContentTemplateSelector}"/>
                                                    <Setter Property="Command">
                                                        <Setter.Value>
                                                            <Binding Path=".">
                                                                <Binding.Converter>
                                                                    <avalondock:ActivateCommandLayoutItemFromLayoutModelConverter/>
                                                                </Binding.Converter>
                                                            </Binding>
                                                        </Setter.Value>
                                                    </Setter>
                                                    <Style.BasedOn>
                                                        <Style TargetType="{x:Type MenuItem}">
                                                            <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type ItemsControl}}}"/>
                                                            <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type ItemsControl}}}"/>
                                                            <Setter Property="Background" Value="Transparent"/>
                                                            <Setter Property="ScrollViewer.PanningMode" Value="Both"/>
                                                            <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
                                                            <Setter Property="Template" Value="{DynamicResource {ComponentResourceKey ResourceId=SubmenuItemTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}"/>
                                                            <Style.Triggers>
                                                                <Trigger Property="Role" Value="TopLevelHeader">
                                                                    <Setter Property="Padding" Value="7,2,8,3"/>
                                                                    <Setter Property="Template" Value="{DynamicResource {ComponentResourceKey ResourceId=TopLevelHeaderTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}"/>
                                                                </Trigger>
                                                                <Trigger Property="Role" Value="TopLevelItem">
                                                                    <Setter Property="Padding" Value="7,2,8,3"/>
                                                                    <Setter Property="Template" Value="{DynamicResource {ComponentResourceKey ResourceId=TopLevelItemTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}"/>
                                                                </Trigger>
                                                                <Trigger Property="Role" Value="SubmenuHeader">
                                                                    <Setter Property="Padding" Value="2,3"/>
                                                                    <Setter Property="Template" Value="{DynamicResource {ComponentResourceKey ResourceId=SubmenuHeaderTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}"/>
                                                                </Trigger>
                                                                <Trigger Property="Role" Value="SubmenuItem">
                                                                    <Setter Property="Padding" Value="2,3"/>
                                                                </Trigger>
                                                            </Style.Triggers>
                                                        </Style>
                                                    </Style.BasedOn>
                                                </Style>
                                            </avalondock:ContextMenuEx.ItemContainerStyle>
                                        </avalondock:ContextMenuEx>
                                    </avalondock:DropDownButton.DropDownContextMenu>
                                    <avalondock:DropDownButton.Style>
                                        <Style TargetType="{x:Type ToggleButton}">

                                            <Setter Property="Padding" Value="2"/>
                                            <Setter Property="BorderThickness" Value="1"/>
                                            <Setter Property="Background" Value="Transparent"/>
                                            <Setter Property="BorderBrush" Value="Transparent"/>
                                            <Setter Property="HorizontalAlignment" Value="Center"/>
                                            <Setter Property="VerticalAlignment" Value="Center"/>
                                            <Setter Property="HorizontalContentAlignment" Value="Center"/>
                                            <Setter Property="VerticalContentAlignment" Value="Center"/>
                                            <Setter Property="Template">
                                                <Setter.Value>
                                                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                                                        <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True">
                                                            <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                                                        </Border>
                                                        <ControlTemplate.Triggers>
                                                            <Trigger Property="IsChecked" Value="True">
                                                                <Setter Property="BorderBrush" Value="#FF3399FF"/>
                                                                <Setter Property="Background" Value="#FFE6F0FA"/>
                                                            </Trigger>
                                                            <Trigger Property="IsMouseOver" Value="True">
                                                                <Setter Property="BorderBrush" Value="#FF3399FF"/>
                                                                <Setter Property="Background" Value="#FFC2E0FF"/>
                                                            </Trigger>
                                                            <Trigger Property="IsKeyboardFocused" Value="True">
                                                                <Setter Property="BorderBrush" Value="#FF3399FF"/>
                                                                <Setter Property="Background" Value="#FFC2E0FF"/>
                                                            </Trigger>
                                                            <MultiTrigger>
                                                                <MultiTrigger.Conditions>
                                                                    <Condition Property="IsMouseOver" Value="True"/>
                                                                    <Condition Property="IsChecked" Value="True"/>
                                                                </MultiTrigger.Conditions>
                                                                <Setter Property="BorderBrush" Value="#FF3399FF"/>
                                                                <Setter Property="Background" Value="#FF99CCFF"/>
                                                            </MultiTrigger>
                                                            <MultiTrigger>
                                                                <MultiTrigger.Conditions>
                                                                    <Condition Property="IsKeyboardFocused" Value="True"/>
                                                                    <Condition Property="IsChecked" Value="True"/>
                                                                </MultiTrigger.Conditions>
                                                                <Setter Property="BorderBrush" Value="#FF3399FF"/>
                                                                <Setter Property="Background" Value="#FF99CCFF"/>
                                                            </MultiTrigger>
                                                            <Trigger Property="IsPressed" Value="True">
                                                                <Setter Property="BorderBrush" Value="#FF3399FF"/>
                                                                <Setter Property="Background" Value="#FF99CCFF"/>
                                                            </Trigger>
                                                            <Trigger Property="IsEnabled" Value="False">
                                                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                                                            </Trigger>
                                                        </ControlTemplate.Triggers>
                                                    </ControlTemplate>
                                                </Setter.Value>
                                            </Setter>
                                        </Style>
                                    </avalondock:DropDownButton.Style>
                                    <!--<Image>
                                        <Image.Source>
                                            <BitmapImage UriSource="pack://application:,,,/AvalonDock;component/Images/PinDocMenu.png"/>
                                        </Image.Source>
                                    </Image>-->
                                </avalondock:DropDownButton>
                            </Grid>
                            <Border x:Name="ContentPanel" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Column="0" KeyboardNavigation.DirectionalNavigation="Contained" HorizontalAlignment="Stretch" Grid.Row="1" KeyboardNavigation.TabIndex="2" KeyboardNavigation.TabNavigation="Cycle" VerticalAlignment="Stretch">
                                <ContentPresenter x:Name="PART_SelectedContentHost" ContentTemplate="{TemplateBinding SelectedContentTemplate}" Content="{TemplateBinding SelectedContent}" ContentStringFormat="{TemplateBinding SelectedContentStringFormat}" ContentSource="SelectedContent" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                            </Border>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                            </Trigger>
                            <DataTrigger Binding="{Binding Model.ChildrenCount, RelativeSource={RelativeSource Self}}" Value="0">
                                <Setter Property="Visibility" TargetName="MenuDropDownButton" Value="Collapsed"/>
                            </DataTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="ItemContainerStyle">
                <Setter.Value>
                    <Style TargetType="{x:Type TabItem}">
                        <Setter Property="Visibility">
                            <Setter.Value>
                                <Binding Path="IsVisible">
                                    <Binding.Converter>
                                        <avalondock:BoolToVisibilityConverter/>
                                    </Binding.Converter>
                                </Binding>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
                        <Setter Property="ToolTip" Value="{Binding ToolTip}"/>
                    </Style>
                </Setter.Value>
            </Setter>
            <Setter Property="ItemTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <avalondock:LayoutDocumentTabItem Model="{Binding}"/>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <avalondock:LayoutDocumentControl Model="{Binding}"/>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        </ResourceDictionary>
    </UserControl.Resources>
    <i:Interaction.Behaviors>
        <beh:FindReplaceBehavior />
    </i:Interaction.Behaviors>
    <!--
<i:Interaction.Triggers>    
        <i:EventTrigger EventName="ContentRendered">
            <cal:ActionMessage MethodName="OnContentRendered" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    -->
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <avalondock:DockingManager x:Name="Document" 
                                   DocumentPaneControlStyle="{DynamicResource CustomLayoutDocumentPaneControlStyle}" 
                                   Margin="0,-1,0,1"
                                   AnchorablesSource="{Binding ToolWindows}"
                                   Grid.Row="0">
            <avalondock:DockingManager.LayoutUpdateStrategy>
                <local:DaxStudioLayoutStrategy/>
            </avalondock:DockingManager.LayoutUpdateStrategy>
            <avalondock:DockingManager.LayoutItemContainerStyleSelector>
                <local:PanesStyleSelector>
                    <local:PanesStyleSelector.ToolStyle>
                        <Style TargetType="{x:Type avalondock:LayoutAnchorableItem}">
                            <Setter Property="Title" Value="{Binding Model.Title}"/>
                            <Setter Property="IconSource" Value="{Binding Model.IconSource}"/>
                            <!--<Setter Property="Visibility" Value="{Binding Model.IsVisible, Mode=TwoWay, Converter={StaticResource BoolToVisibilityConverter}, ConverterParameter={x:Static Visibility.Hidden}}"/>-->
                            <Setter Property="ContentId" Value="{Binding Model.ContentId}"/>
                            <Setter Property="IsSelected" Value="{Binding Model.IsSelected, Mode=TwoWay}"/>
                            <Setter Property="IsActive" Value="{Binding Model.IsActive, Mode=TwoWay}"/>
                            <Setter Property="CanClose" Value="{Binding Model.CanClose}" />
                            <Setter Property="CanHide" Value="{Binding Model.CanHide}"/>
                        </Style>
                    </local:PanesStyleSelector.ToolStyle>
                    </local:PanesStyleSelector>
                </avalondock:DockingManager.LayoutItemContainerStyleSelector>
            <!--
            <avalondock:DockingManager.AnchorablePaneTemplate>
                <ControlTemplate>
                    <ContentControl cal:View.Model="{Binding . }" IsTabStop="False" />
                </ControlTemplate>
            </avalondock:DockingManager.AnchorablePaneTemplate>
            -->
            <avalondock:DockingManager.AnchorableTitleTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding DisplayName}"></TextBlock>
                </DataTemplate>
            </avalondock:DockingManager.AnchorableTitleTemplate>
            <avalondock:DockingManager.LayoutItemTemplateSelector>

                <local:AutobinderTemplateSelector>
                    <local:AutobinderTemplateSelector.Template>
                        <DataTemplate>
                            <ContentControl cal:View.Model="{Binding . }" IsTabStop="False" />
                        </DataTemplate>
                    </local:AutobinderTemplateSelector.Template>
                </local:AutobinderTemplateSelector>
            </avalondock:DockingManager.LayoutItemTemplateSelector>
            
<!-- cal:Message.Attach="[Event DocumentClosing]=[Action DocumentClosing($this)]" -->

            <avalondock:LayoutRoot>
                <!-- Left side Tool Panes -->
                <avalondock:LayoutPanel Orientation="Horizontal">
                    <avalondock:LayoutAnchorablePaneGroup DockMinWidth="150" DockWidth="200">
                        <avalondock:LayoutAnchorablePane Name="DockLeft">
                            <!--
                            <avalondock:LayoutAnchorable Title="MetaData" CanClose="False" CanHide="False">
                                <ContentControl cal:View.Model="{Binding MetadataPane}" cal:View.ApplyConventions="true"></ContentControl>
                            </avalondock:LayoutAnchorable>
                            <avalondock:LayoutAnchorable Title="Functions" CanClose="False" CanHide="False">
                                <ContentControl cal:View.Model="{Binding FunctionPane}" cal:View.ApplyConventions="true"></ContentControl>
                            </avalondock:LayoutAnchorable>
                            <avalondock:LayoutAnchorable Title="DMV" CanClose="False" CanHide="False">
                                <ContentControl cal:View.Model="{Binding DmvPane}" cal:View.ApplyConventions="true"></ContentControl>
                            </avalondock:LayoutAnchorable>
                            -->
                            
                        </avalondock:LayoutAnchorablePane>
                    </avalondock:LayoutAnchorablePaneGroup>
                    
                    <!-- Main Document Pane -->
                    
                    <avalondock:LayoutPanel Orientation="Vertical">
                        
                            <avalondock:LayoutDocumentPane DockHeight="Auto"  >
                            <avalondock:LayoutDocument CanFloat="False"  CanClose="False">
                                
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*"/>
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="*"/>
                                    </Grid.RowDefinitions>

                                    <dax:DAXEditor Margin="2,2,2,2" x:Name="daxEditor" Padding="8,8,8,8"
                                                   Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Grid.RowSpan="2"
                                                   VerticalAlignment="Top" MinWidth="100" MinHeight="80"
                                                   FontSize="{Binding Path=SizeUnitLabel.ScreenPoints, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                                                   
                                                   Document ="{Binding Document, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                                   
                                                   EditorSelectedText="{Binding SelectedText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                                   EditorSelectionStart="{Binding SelectionStart, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                                   EditorSelectionLength="{Binding SelectionLength,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                                   EditorCaretOffset="{Binding CaretOffset}" 
                                                   attached:DaxEditorFocusExtensions.IsFocused="{Binding IsFocused}"
                                                   >
                                        
                                        <ContextMenuService.ContextMenu>
                                        <ContextMenu>
                                            <MenuItem x:Name="Cut"  Header="Cut" Icon="{StaticResource CutIcon}"
                                                      cal:Message.Attach="Cut()"></MenuItem>
                                            <MenuItem x:Name="Copy" Header="Copy" Icon="{StaticResource CopyIcon}"
                                                      cal:Message.Attach="Copy()"></MenuItem>
                                            <MenuItem x:Name="Paste" Header="Paste" Icon="{StaticResource PasteIcon}"
                                                      cal:Message.Attach="Paste()"></MenuItem>
                                            <Separator />
                                            <MenuItem x:Name="CommentSelection" Header="Comment" Icon="{StaticResource CommentIcon}"
                                                      cal:Message.Attach="CommentSelection()"></MenuItem>
                                            <MenuItem x:Name="UncommentSelection" Header="Uncomment" Icon="{StaticResource UncommentIcon}"
                                                      cal:Message.Attach="UnCommentSelection()"></MenuItem>
                                        </ContextMenu>
                                    </ContextMenuService.ContextMenu>
                                    
                                </dax:DAXEditor>
                                <ContentControl x:Name="FindReplaceDialog" 
                                                HorizontalAlignment="Right" 
                                                VerticalAlignment="Top"
                                                Width="300"
                                                Margin="0,2,21,0" 
                                                Grid.Column="1" Grid.Row="0"
                                                cal:View.Model="{Binding FindReplaceDialog}" cal:View.ApplyConventions="true"/>
                                </Grid>
                            </avalondock:LayoutDocument>
                        </avalondock:LayoutDocumentPane>
                    
                        
                    <!-- Bottom Output panes -->
                        
                        <avalondock:LayoutAnchorablePaneGroup Orientation="Vertical" DockMinHeight="170" DockHeight="260">

                            <avalondock:LayoutAnchorablePane Name="DockBottom" DockMinHeight="170">
                                <!--
                                <avalondock:LayoutAnchorable x:Name="OutputAnchorable"  Title="Output"   CanClose="False" CanHide="False" AutoHideMinHeight="100">
                                    <ContentControl x:Name="Output" cal:View.Model="{Binding OutputPane}" cal:View.ApplyConventions="true"></ContentControl>
                                </avalondock:LayoutAnchorable>
                                <avalondock:LayoutAnchorable x:Name="QueryResultsAnchorable" Title="Results"  CanClose="False" CanHide="False" AutoHideMinHeight="100">
                                    <ContentControl x:Name="QueryResults" cal:View.Model="{Binding QueryResultsPane}" ></ContentControl>
                                </avalondock:LayoutAnchorable>
                                -->
                            </avalondock:LayoutAnchorablePane>
                        </avalondock:LayoutAnchorablePaneGroup>
                    </avalondock:LayoutPanel>

                </avalondock:LayoutPanel>

            </avalondock:LayoutRoot>
        </avalondock:DockingManager>
        
    </Grid>
</UserControl>
